package eu.tsp.isensors.test.communication;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Provides;
import org.apache.felix.ipojo.annotations.ServiceProperty;

@Component
@Provides
public class BlogServlet extends RestServlet{

   @ServiceProperty
   private String alias; // In the instance configuration
  
   /**
    * GET an entry. The base class will populate the "entryId" attribute before calling invoke.
    * We told it to do so below, when we defined entryIdentifier.
    */
   RequestHandler entryGetHandler = new RequestHandler(){

       public void invoke(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
           try {
               // The base class has parsed the URI and populated the request with
               // the "entryId" attribute
               String entryId = (String)request.getAttribute("entryId");
	       System.out.println(">> entryGetHandler "+entryId+"*"+ request.getRequestURI() + "*" + request.getServletPath()+"*");
               String forward = "/WEB-INF/entry.jsp";
               request.setAttribute("entryHTML", getEntryHTML(entryId));
               request.setAttribute("entryTitle", getEntryTitle(entryId));
              
               request.getRequestDispatcher(forward).forward(request, response);
           } catch (Exception e) {
               response.sendError(404);
           }
       }         
   };
  
   
   /**
    * POST a new entry to the collection URI
    */
   RequestHandler collectionPostHandler = new RequestHandler(){

       public void invoke(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
           try {
               String entryId = saveEntry(request, response);  
              
               response.setHeader("Location", buildEntryUri(entryId));
               response.setStatus(201);
               //write out some HTML or maybe the entry itself.
               String forward = "/WEB-INF/entry.jsp";
               request.setAttribute("entryHTML", getEntryHTML(entryId));
               request.setAttribute("entryTitle", getEntryTitle(entryId));
              request.getRequestDispatcher(forward).forward(request, response);
           } catch (Exception e) {
               response.sendError(404);
           }
       }         
   };
  
   //Stubbing these in...
   RequestHandler entryPutHandler =null;
   RequestHandler entryDeleteHandler = null;
   RequestHandler collectionGetHandler = null;

   //
   // One ResourceIdentifier per URI pattern. 
   // Each one tells us how to parse the pattern into attributes, and handlers
   // for HTTP methods on the resources it identifies.
   //
   ResourceIdentifier entryIdentifier = new ResourceIdentifier(       
           "^/(\\d+)$",                // URI pattern
           new String[] {"entryId"},   // match pattern and insert named request attributes
           entryGetHandler,            // GET handler for entry URIs
           null,                       // no POST handler for entries
           entryPutHandler,            // PUT an entry
           entryDeleteHandler);        // DELETE an entry
                                      
  
   ResourceIdentifier collectionIdentifier = new ResourceIdentifier(
           "^/$",                      // URI pattern
           collectionGetHandler,            // GET handler for collection would list entries
           collectionPostHandler);          // POST handler for collection will add and entry
                                       
  

   @Override
   /**
    * Here's how we tell our base class how to map URIs to handlers:
    */
   protected ResourceIdentifier[] resourceIdentifiers() {      
       return new ResourceIdentifier[]{entryIdentifier, collectionIdentifier};
   }

   // these are just stubs... exercise for the reader.
   private String buildEntryUri(String entryId) {
       return null;
   }

   private String saveEntry(HttpServletRequest request,
           HttpServletResponse response) {
       return null;
   }

   private Object getEntryTitle(String entryId) {
       return null;
   }
   private Object getEntryHTML(String entryId) {
       return null;
   }
  
}
